//
//  SWCheckData.m
//  SerialPort
//
//  Created by MT-SW-003 on 2021/4/9.
//  Copyright © 2021 MT-SW-003. All rights reserved.
//

#import "SWCheckData.h"

@implementation SWCheckData


/*
* Init.
*
* @param serialParamData The jsonData of serial param from serialConfig.json file.
*
* @param configData  The jsonData of serial config from config.json file.
*
* @return Instance of SWCheckData.
*/

- (id)initWithSerialData:(id)serialParamData withConfigData:(id)configData {
    if ((self = [super init]) != nil) {
        //set serial config
        devPath = [configData objectForKey:@"dev"];
        baudRate = [[configData objectForKey:@"baudRate"] intValue];
        dataBit = [[configData objectForKey:@"dataBit"] intValue];
        parity = [configData objectForKey:@"parity"];
        stopBit = [[configData objectForKey:@"stopBit"] intValue];
        
        //set serial param list
        baudRateArray = [serialParamData objectForKey:@"baudRate"];
        dataBitArray = [serialParamData objectForKey:@"dataBit"];
        stopBitArray = [serialParamData objectForKey:@"stopBit"];
        parityArray = [serialParamData objectForKey:@"parity"];
    }
    return self;
}


/*
* check config data of the serial port
*
* @return TRUE or FALSE BOOL of check result.
*/
- (BOOL)checkConfigData{
    if([self checkBaudRate: baudRate]
        && [self checkDataBit: dataBit]
        && [self checkParity: parity]
        && [self checkStopBit: stopBit] ) {
        return TRUE;
    }
    else {
        return FALSE;
    }
}


/*
* CheckBaudRate
*
* @param baudRate.
*
* @return TRUE or FALSE BOOL of check baudRate result.
*/
- (BOOL)checkBaudRate:(int)baudRate {
    NSNumber *intBaudRate = [NSNumber numberWithInt: baudRate];
    BOOL isInParamList = [baudRateArray containsObject: intBaudRate];
    if (!isInParamList) {
        NSLog(@"The baudRate: %d is not in the available list", baudRate);
    }
    return isInParamList;
}


/*
* Check dataBit
*
* @param dataBit.
*
* @return TRUE or FALSE BOOL of check dataBit result.
*/
- (BOOL)checkDataBit:(int)dataBit {

    NSNumber *intDataBit = [NSNumber numberWithInt: dataBit];
    BOOL isInParamList = [dataBitArray containsObject: intDataBit];
    if (!isInParamList) {
        NSLog(@"The dataBit: %d is not in the available list", dataBit);
    }
    return isInParamList;
}

/*
* Check parity
*
* @param checkParity.
*
* @return TRUE or FALSE BOOL of check parity result.
*/
- (BOOL)checkParity:(NSString *)parity {
   
    NSString *strParity = [NSString stringWithFormat:@"%@",parity];
    BOOL isInParamList = [parityArray containsObject: strParity];
    if (!isInParamList) {
        NSLog(@"The parity: %@ is not in the available list", parity);
    }
    return isInParamList;
}

/*
* Check stopBit
*
* @param stopBit.
*
* @return TRUE or FALSE BOOL of check stopBit result.
*/
- (BOOL)checkStopBit:(int)stopBit {
    
    NSNumber *intStopBit = [NSNumber numberWithInt: stopBit];
    BOOL isInParamList = [stopBitArray containsObject: intStopBit];
    if (!isInParamList) {
        NSLog(@"The stopBit: %d is not in the available list", stopBit);
    }
    return isInParamList;
}


/*
 * cheack write input data
 */
- (BOOL)checkWriteData:(char *)inputWriteData {
    if (1==1) {
        return TRUE;
    }
    else {
        return FALSE;
    }
}


/*
* cheack write input data
*/
- (BOOL)checkReadData:(char *)readData andExpectedData:(char *)expectedData {
    if (1==1) {
        return TRUE;
    }
    else {
        return FALSE;
    }
}

/* getter baudRate, dataBit, stopBit */
@synthesize baudRate, dataBit, stopBit;

/* getter parity */
@synthesize parity;

/* getter devPath */
@synthesize devPath;

/* getter serial param array */
@synthesize  baudRateArray, dataBitArray, stopBitArray, parityArray;

@end
